package com.hjm.app.fragment.order;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hjm.app.R;
import com.hjm.ui.order.OrderItem;
import com.hjm.ui.order.adapter.OrderItemAdapter;
import com.hjm.ui.order.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class FragmentOrderBase extends Fragment {
    static final String ORDER_LIST = "order_list";
    private static final String TAG = "FragmentOrderBase";
    List<? extends Order> orderList;

    OrderItem orderItem;
    OrderItemAdapter.OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OrderItemAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
        if (orderItem != null){
            orderItem.setOnItemClickListener(onItemClickListener);
        }
    }

    public void setOrderList(List<? extends Order> orderList) {
        this.orderList = orderList;
        if (orderItem != null){
            Log.d(TAG, "setOrderList: 设置到订单项 "+this.orderList.size());
            orderItem.setOrderList(this.orderList);
        }
    }

    public FragmentOrderBase() {
        // Required empty public constructor
    }


    // 提供一个静态方法来帮助子类创建Fragment实例并设置参数
    public static FragmentOrderBase newInstance(List<? extends Order> orderList) {
        try {
            FragmentOrderBase fragment = new FragmentOrderBase();
            Bundle args = new Bundle();
            args.putParcelableArrayList(ORDER_LIST, (ArrayList<? extends Parcelable>) orderList);
            fragment.setArguments(args);
            return fragment;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            this.orderList = getArguments().getParcelableArrayList(ORDER_LIST);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_base, container, false);

        orderItem = view.findViewById(R.id.order_item_base);
        if (onItemClickListener != null)
            orderItem.setOnItemClickListener(onItemClickListener);
        if (orderList != null){
            Log.d(TAG, "onCreateView: 设置到订单项 "+orderList.size());
            orderItem.setOrderList(orderList);
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}
